#include<iostream>
#include<map>
using namespace std;
int n,k,father[50001],i,jia=0,a,b,c,b1,c1;
map<int,int>d;
void init();
int get(int x);
int main(){
	cin>>n>>k;
	init();
	while(k--){
		cin>>a>>b>>c;
		if(b>n||b<1||c>n||c<1){
			jia++;continue;
		}
		b1=get(b);c1=get(c);
		if(a==1){
			if(d[b1]==c1||d[c1]==b1){
				jia++;continue;
			}
			father[b1]=c1;
			d[b]=d[b1];
		}
		if(a==2){
			if(d[c1]==b1||c1==b1){
				jia++;continue;
			}
			d[b1]=c1;
		}
	}
	cout<<jia;
	return 0;
}
void init(){
	for(i=1;i<=n;++i){
		father[i]=i;
	}
}
int get(int x){
	if(x==father[x]) return x;
	return father[x]=get(father[x]);
}